<?php

namespace Happy\CommandsService\Database\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ServerModel extends Model
{
    use HasFactory;

    public $table = 'servers';

    public $timestamps = false;

    protected $primaryKey = 'ip_address';

    protected $keyType = 'string';

    public function hasActive()
    {
        return $this->sessions()->where('active', '=', true)->count();
    }

    public function sessions()
    {
        return $this->hasMany(SessionsModel::class, 'server', 'ip_address');
    }

    public function services()
    {
        return $this->belongsToMany(ServiceModel::class,
            'servers_services',
            'server',
            'service');
    }
}
